import {
  addPersonBill, addPersonBillCategory,
  deletePersonBill, deletePersonBillCategory,
  getPersonBill,
  getPersonBillCategory,
  updatePersonBill
} from "@/api/bill/person/api.js";
import {getTodayAndMonthZeroTime, getTodayAndYesterdayZeroTime} from "@/utils/index.js";
import SafeCalc from "@/utils/bignumberjs.js";

// -----------------------------------------------------------1---------------------------------------------------------------------------------------
// 个人的所有账单
const personBill = ref([]);

const initPersonBill = async () => {
  let personBillRes = await getPersonBill(localStorage['id']);
  let personBillTemp = personBillRes.bills.map(item => {
    return {
      id: item.id,
      category: item.category,
      amount: item.amount,
      remark: item.remark,
      createTime: Number(item.create_time),
      isComplete: item.is_complete,
      isDelete: item.is_delete,
      billTime: Number(item.bill_time),
    };
  });
  // 按照billTime 从大到小排序
  personBillTemp.sort((a, b) => b.billTime - a.billTime);
  personBill.value = personBillTemp;
};

export const getPersonBillRef = () => {
  return personBill;
};
export const usePersonBillDelete = async (billId) => {
  await deletePersonBill(localStorage['id'], billId);
  let bill = personBill.value.find(item => item.id === billId);
  if(bill) {
    bill.isDelete = 1;
  }
};
// todo 新增billTime
export const usePersonBillAdd = async (category, remark, amount, isComplete, billTime) => {
  let bill = (await addPersonBill(localStorage['id'], category, remark, amount, isComplete, billTime)).bill;
  personBill.value =  [{
    id: bill.id,
    category: bill.category,
    amount: bill.amount,
    remark: bill.remark,
    createTime: Number(bill.create_time),
    isComplete: bill.is_complete,
    isDelete: bill.is_delete,
    billTime: Number(bill.bill_time),
  }, ...personBill.value];
};

// todo 新增billTime
export const usePersonBillUpdate = async (billId, category, remark, amount, isComplete, billTime) => {
  let bill = (await updatePersonBill(localStorage['id'], billId, category, remark, amount, isComplete, billTime)).bill;
  let billIndex = personBill.value.findIndex(item => item.id === billId);
  if(billIndex !== -1) {
    personBill.value[billIndex] = {
      id: bill.id,
      category: bill.category,
      amount: bill.amount,
      remark: bill.remark,
      createTime: Number(bill.create_time),
      isComplete: bill.is_complete,
      isDelete: bill.is_delete,
      billTime: Number(bill.bill_time),
    };
  }
};

initPersonBill();

// -----------------------------------------------------------2---------------------------------------------------------------------------------------
// 展示在工作台的个人数据
const personData = ref({
  id: localStorage['id'],
  yesterdayCount: 0,
  monthCount: 0,
  monthOut: 0,
  monthIn: 0
});

export const getPersonDataRef = () => {
  return personData;
};

watchEffect(() => {
  let {today, yesterday} = getTodayAndYesterdayZeroTime();
  let yesterdayCount = personBill.value.filter(item => item.createTime >= yesterday && item.createTime <= today).length;
  let {month} = getTodayAndMonthZeroTime();
  let monthCount = personBill.value.filter(item => item.createTime >= month).length;
  personData.value.yesterdayCount = yesterdayCount;
  personData.value.monthCount = monthCount;
});


// -----------------------------------------------------------3---------------------------------------------------------------------------------------
// 个人账单分类
const personBillCategory = ref({});
const initPersonBillCategory = async () => {
  const categoryListRes = await getPersonBillCategory(localStorage['id']);
  let categoryTemp = {};
  categoryListRes.categories.forEach(item => {
    categoryTemp[item.id] = {
      id: item.id,
      name: item.name,
      isOut: item.is_out
    };
  });
  personBillCategory.value = categoryTemp;
};

export const getPersonBillCategoryRef = () => {
  return personBillCategory;
};

export const usePersonDeleteBillCategory = async (id) => {
  await deletePersonBillCategory(localStorage['id'], id);
  let categoryTemp = {...personBillCategory.value};
  delete categoryTemp[id];
  personBillCategory.value = categoryTemp;
};

export const usePersonAddBillCategory = async (name, isOut) => {
  let categoryTemp = {...personBillCategory.value};
  let categoryNew = (await addPersonBillCategory(localStorage['id'], name, isOut)).category;
  categoryTemp[categoryNew.id] = {
    id: categoryNew.id,
    name: categoryNew.name,
    isOut: categoryNew.is_out
  };
  personBillCategory.value = categoryTemp;
};

watchEffect(() => {
  if (Object.keys(personBillCategory.value).length === 0 || personBill.value.length === 0) {
    return;
  }
  let {month} = getTodayAndMonthZeroTime();
  let monthOut = personBill.value.filter(item => item.createTime >= month).reduce((pre, cur) => {
    if (personBillCategory.value[cur.category] && personBillCategory.value[cur.category].isOut && cur.isDelete === 0) {
      return SafeCalc.add(pre, cur.amount);
    }
    return pre;
  }, '0');
  let monthIn = personBill.value.filter(item => item.createTime >= month).reduce((pre, cur) => {
    if (personBillCategory.value[cur.category] && !personBillCategory.value[cur.category].isOut && cur.isDelete === 0) {
      return SafeCalc.add(pre, cur.amount);
    }
    return pre;

  }, '0');
  personData.value.monthIn = monthIn;
  personData.value.monthOut = monthOut;
});


initPersonBillCategory();
